package com.pd.modules.server.dialogs;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JDialog;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import com.pd.modules.common.User;
import com.pd.modules.server.database.DBUsersHandler;
import com.pd.modules.server.database.DataBase;

@SuppressWarnings("serial")
public class DialogUsers extends JDialog implements Observer
{

	private final JPanel contentPanel = new JPanel();
	private JTable mTable;

	private String[] mTableColumns = new String[] { "Username", "Password" };
	private DefaultTableModel mTableModel = null;
	private JMenuItem mMenuItemDelete;

	private void showAllUsers()
	{
		DBUsersHandler db = DataBase.getInstance().Users();

		User[] users = db.getAllUsers();

		Object[][] rows = new Object[users.length][2];
		for (int i = 0; i < users.length; i++)
		{
			rows[i] = users[i].getAsTableRow();
		}

		refreshTable(rows);
	}

	private void refreshTable(Object[][] _data)
	{
		mTableModel = new DefaultTableModel(_data, mTableColumns);
		mTable.setModel(mTableModel);

		// Alinha o texto ao centro
		DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
		dtcr.setHorizontalAlignment(SwingConstants.CENTER);
		for (String columnName : mTableColumns)
			mTable.getColumn(columnName).setCellRenderer(dtcr);
	}

	/**
	 * Create the dialog.
	 */
	public DialogUsers()
	{
		setTitle("Gest\u00E3o de Utilizadores");
		setResizable(false);
		setModalityType(ModalityType.APPLICATION_MODAL);
		setModal(true);
		setBounds(100, 100, 404, 300);

		DataBase.getInstance().Users().addObserver(this);

		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane);
			{
				mTable = new JTable()
				{
					@Override
					public boolean isCellEditable(int rowIndex, int colIndex)
					{
						return false; // Disallow the editing of any cell
					}
				};
				mTable.setModel(new DefaultTableModel(null, mTableColumns));
				mTable.getSelectionModel().addListSelectionListener(new EventSelectionChanged());
				mTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				scrollPane.setViewportView(mTable);
				showAllUsers();
			}
		}
		{
			JMenuBar menuBar = new JMenuBar();
			setJMenuBar(menuBar);
			{
				JMenu mnFicheiro = new JMenu("Ficheiro");
				menuBar.add(mnFicheiro);
				{
					JMenuItem mntmSair = new JMenuItem("Sair");
					mntmSair.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
							setVisible(false);
						}
					});
					mnFicheiro.add(mntmSair);
				}
			}
			{
				JMenu mnEditar = new JMenu("Editar");
				menuBar.add(mnEditar);
				{
					JMenuItem mntmNovo = new JMenuItem("Novo...");
					mntmNovo.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
							DialogCreateUser dialog = new DialogCreateUser();
							dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
							dialog.setVisible(true);
						}
					});
					mnEditar.add(mntmNovo);
				}
				{
					mMenuItemDelete = new JMenuItem("Apagar");
					mMenuItemDelete.setEnabled(false);
					mMenuItemDelete.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
							int idx = mTable.getSelectedRow();
							String username = (String) mTable.getModel().getValueAt(idx, 0);

							// Apaga o utilizador da base de dados
							DataBase.getInstance().Users().deleteUser(username);

							mMenuItemDelete.setEnabled(false);
						}
					});
					mnEditar.add(mMenuItemDelete);
				}
			}
		}
	}

	class EventSelectionChanged implements ListSelectionListener
	{

		@Override
		public void valueChanged(ListSelectionEvent e)
		{
			mMenuItemDelete.setEnabled(true);
		}
	}

	@Override
	public void update(Observable arg0, Object arg1)
	{
		showAllUsers();
	}

}
